package com.example.myapplication.ContentTest;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ContentResolver;

import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import com.example.myapplication.ArrayAdapterInclude.ContactArrayListAdapter;
import com.example.myapplication.ArrayAdapterInclude.IntentArrayListAdapter;
import com.example.myapplication.Person;
import com.example.myapplication.R;
import androidx.core.app.ActivityCompat;
import java.util.ArrayList;

public class ContactsInfoFragment extends Fragment {
  private static final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 1;

  private ArrayList<ContactInfo> contactArrayList = new ArrayList<>();
  ListView listView;
  View view;
  public ContactsInfoFragment() {


  }

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (ContextCompat.checkSelfPermission(this.getContext(), Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(this.getActivity(),new String[]{Manifest.permission.READ_CONTACTS},
          MY_PERMISSIONS_REQUEST_READ_CONTACTS);

    }





  }

  @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    GetData();
    //需要重新实现一个Adapter
    ShowContacts();
  }

  private void GetData() {

    ContentResolver resolver = getContext().getContentResolver();
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    Cursor cursor = resolver.query(uri, null, null, null, null);
    while (cursor.moveToNext()) {
      @SuppressLint("Range") String cName = cursor.getString(
          cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
      @SuppressLint("Range") String cNum =
          cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
      ContactInfo contactInfo = new ContactInfo(cName, cNum);
      contactArrayList.add(contactInfo);
    }

  }

  @Nullable @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.contact_info_fragment_layout, container, false);
    return view;
  }

  private void ShowContacts() {
    listView = (ListView) view.findViewById(R.id.listView);
    ContactArrayListAdapter adapter =
        new ContactArrayListAdapter(this.getContext(), contactArrayList);
    listView.setAdapter(adapter);

  }

    @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
      @NonNull int[] grantResults) {
      if (requestCode == MY_PERMISSIONS_REQUEST_READ_CONTACTS) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

          Toast.makeText(this.getContext(), "获取权限成功", Toast.LENGTH_SHORT).show();
          GetData();
          ShowContacts();
        } else {
          Toast.makeText(this.getContext(), "获取权限失败", Toast.LENGTH_SHORT).show();
          getActivity().finish();
        }
      }
  }
}
